
import java.io.IOException;
import java.util.Vector;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class AnswerParser
{
    public String error = "";
    public String ecode = "";
    public String obj = "";
    public String act = "";

    public Vector items = null;

    public AnswerParser(Vector Items)
    {
        this.items = Items;
    }

    public void execute(KXmlParser parser)
            throws XmlPullParserException, IOException
    {
        boolean inUniItem = false;

        items.removeAllElements();
        UniItem item = null;

        int eventType = KXmlParser.START_TAG;
        eventType = parser.next();
        while (eventType != KXmlParser.END_DOCUMENT)
        {

            eventType = parser.next();
            if (eventType == KXmlParser.START_TAG)
            {
//                System.out.println("parsing->start tag: "+ parser.getName().toLowerCase());
                if (parser.getName().toLowerCase().equals("ans"))
                {
                    error = parser.getAttributeValue("", "err");
                    ecode = parser.getAttributeValue("", "ecode");
                    obj = parser.getAttributeValue("", "o");
                    act = parser.getAttributeValue("", "a");
                    continue;
                }
                else if(parser.getName().equals("n"))
                {
                    inUniItem = true;
                    item = parseUniversal(parser);
                }
            }
            else if(eventType == KXmlParser.TEXT)
            {
//                System.out.println("parsing->text: "+ parser.getText());
                if(inUniItem && (item != null))
                    item.add("text", parser.getText());
            }
            else if(eventType == KXmlParser.END_TAG)
            {
//                System.out.println("parsing->end tag: "+ parser.getName().toLowerCase());
                if(parser.getName().equals("n"))
                {
                    inUniItem = false;
                    if(item != null) items.addElement(item);
                }
            }
        }
    }

    private UniItem parseUniversal(KXmlParser Parser)
    {
        UniItem item = new UniItem();
        int n = Parser.getAttributeCount();
        for(int i = 0; i < n; i++)
            item.add(Parser.getAttributeName(i), Parser.getAttributeValue(i));
        return item;
    }
}
